<template>
  <div style="padding: 15px;">
    <div style="margin-bottom: 15px;">
      <Input style="width: 200px;" placeholder="请输入管理账号" v-model="searchloginName"/>
      <Input style="width: 200px;" placeholder="请输入管理员真实姓名" v-model="searchStaffName"/>
      <Select style="width:200px" placeholder="请选择状态" v-model="searchActive" @on-change="searchActiveChange">
        <Option value="1">正常</Option>
        <Option value="0">禁用</Option>
      </Select>
      <Button @click="searchOrg" type="success">搜索</Button>
      <Button @click="add" type="primary" style="float: right;">添加机构管理员</Button>
    </div>
    <Table border ref="selection" :columns="adminPageTitle" :data="adminPageList"></Table>
    <Page :total="total" show-sizer style="margin-top: 15px;" @on-change="pageChange" @on-page-size-change="pageSizeChange"/>
    <Modal v-model="adminPageModal" :title="modalTitle" @on-ok="submit" @on-cancel="cancel">
      <Form :model="adminPageForm" :label-width="80">
        <FormItem label="管理账号">
          <Input v-model="adminPageForm.loginName" placeholder="请输入管理账号"/>
        </FormItem>
        <FormItem label="真实姓名">
          <Input v-model="adminPageForm.staffName" placeholder="请输入真实姓名"/>
        </FormItem>
        <FormItem label="联系电话">
          <Input v-model="adminPageForm.mobile" placeholder="请输入联系电话"/>
        </FormItem>
        <FormItem label="所属机构">
          <Select v-model="adminPageForm.organization">
            <Option v-for="item in orgList" :value="item.id" :key="item.id">{{ item.name }}</Option>
          </Select>
        </FormItem>
        <FormItem label="状态">
          <i-switch size="large" v-model="adminPageForm.active">
            <span slot="open">启用</span>
            <span slot="close">禁用</span>
          </i-switch>
        </FormItem>
      </Form>
    </Modal>
  </div>
</template>

<script>
  import {formatDate} from '_com/date'
  export default {
    name: 'index',
    data () {
      return {
        searchloginName: '',
        searchStaffName: '',
        searchActive: '',
        total: 0,
        number: 0,
        size: 10,
        adminPageForm: {
          loginName: '',
          staffName: '',
          mobile: '',
          organization: [],
          active: true
        },
        orgList: [],
        adminPageModal: false,
        modalTitle: '',
        adminPageTitle: [
          {
            type: 'selection',
            width: 60,
            align: 'center'
          },
          {
            title: '管理账号',
            key: 'loginName',
            align: 'center'
          },
          {
            title: '真实姓名',
            key: 'staffName',
            align: 'center'
          },
          {
            title: '所属机构',
            key: 'orgName',
            align: 'center'
          },
          {
            title: '联系电话',
            key: 'mobile',
            align: 'center'
          },
          {
            title: '创建时间',
            key: 'createTime',
            align: 'center',
            width: 150,
            render: (h, params) => {
              return h('div', formatDate(new Date(params.row.createTime), 'yyyy-MM-dd hh:mm'))
            }
          },
          {
            title: '添加人',
            key: 'createName',
            align: 'center'
          },
          {
            title: '状态',
            key: 'status',
            width: 120,
            align: 'center',
            render: (h, params) => {
              const row = params.row
              const color = row.active === 1 ? 'primary' : row.active === 0 ? 'error' : ''
              const text = row.active === 1 ? '正常' : row.active === 0 ? '禁用' : ''
              return h('Tag', {
                props: {
                  type: 'dot',
                  color: color
                }
              }, text)
            }
          },
          {
            title: '操作',
            key: 'action',
            width: 250,
            align: 'center',
            render: (h, params) => {
              return h('div', [
                h('Button', {
                  props: {
                    type: 'info',
                    size: 'small'
                  },
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: () => {
                      this.$Modal.confirm({
                        title: '重置密码',
                        content: '你确定要重置吗？',
                        onOk: () => {
                          this.resetPassword(params.row.id)
                        }
                      })
                    }
                  }
                }, '重置密码'),
                h('Button', {
                  props: {
                    type: 'primary',
                    size: 'small'
                  },
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: () => {
                      let row = params.row
                      this.editor()
                      this.adminPageForm.loginName = row.loginName
                      this.adminPageForm.staffName = row.staffName
                      this.adminPageForm.mobile = row.mobile
                      this.adminPageForm.organization = [row.orgId]
                      this.adminPageForm.id = row.id
                      if (row.active === 1) {
                        this.adminPageForm.active = true
                      } else {
                        this.adminPageForm.active = false
                      }
                    }
                  }
                }, '编辑'),
                h('Button', {
                  props: {
                    type: 'error',
                    size: 'small'
                  },
                  on: {
                    click: () => {
                      this.$Modal.confirm({
                        title: '删除',
                        content: '你确定要删除吗？',
                        onOk: () => {
                          this.remove(params.row.id)
                        }
                      })
                    }
                  }
                }, '删除')
              ])
            }
          }
        ],
        adminPageList: []
      }
    },
    mounted() {
      this.affiliationList()
      this.adminPage()
    },
    methods: {
      affiliationList() {
        let url = '/api/api/sys/org/list'
        this.$get(url).then((res) => {
          if (res.code === 0) {
            this.orgList = res.data
          }
        })
      },
      adminPage() {
        let url = '/api/api/org/staff/page'
        let form = {
          loginName: this.searchloginName,
          staffName: this.searchStaffName,
          active: this.searchActive,
          number: this.number,
          size: this.size
        }
        this.$get(url, form).then((res) => {
          if (res.code === 0) {
            this.adminPageList = res.data
            this.total = res.total
          }
        })
      },
      title(title) {
        this.adminPageModal = true
        this.modalTitle = title
      },
      add() {
        this.title('添加机构管理员')
      },
      editor() {
        this.title('编辑机构管理员')
      },
      submit() {
        if (this.modalTitle === '添加机构管理员') {
          this.addOrg()
        } else {
          this.theEditor()
        }
      },
      addOrg() {
        let url = '/api/api/org/staff/add'
        let form = {
          loginName: this.adminPageForm.loginName,
          mobile: this.adminPageForm.mobile,
          staffName: this.adminPageForm.staffName,
          orgId: this.adminPageForm.organization,
          active: Number(this.adminPageForm.active)
        }
        this.$post(url, form).then((res) => {
          if (res.code === 0) {
            this.adminPage()
            this.cancel()
            this.$Modal.info({
              title: '添加成功',
              content: '默认密码为：123456'
            })
          } else {
            this.$Message.error('添加失败')
          }
        })
      },
      theEditor() {
        let url = '/api/api/org/staff/edit'
        let form = {
          loginName: this.adminPageForm.loginName,
          mobile: this.adminPageForm.mobile,
          staffName: this.adminPageForm.staffName,
          orgId: this.adminPageForm.organization,
          active: Number(this.adminPageForm.active),
          id: this.adminPageForm.id
        }
        this.$put(url, form).then((res) => {
          if (res.code === 0) {
            this.$Message.success('修改成功')
            this.adminPage()
            this.cancel()
          } else {
            this.$Message.error('修改失败')
          }
        })
      },
      resetPassword(id) {
        let url = '/api/api/org/staff/resertPassword'
        this.$get(url, {id: id}).then((res) => {
          if (res.code === 0) {
            this.$Modal.info({
              title: '密码重置',
              content: '重置密码为：123456'
            })
          } else {
            this.$Message.error('重置失败')
          }
        })
      },
      remove(id) {
        let url = '/api/api/org/staff/delete'
        this.$deletes(url, {id: id}).then((res) => {
          if (res.code === 0) {
            this.$Message.success('删除成功')
            this.adminPage()
          } else {
            this.$Message.error('删除失败')
          }
        })
      },
      searchOrg () {
      },
      cancel() {
        this.adminPageForm = {}
        this.adminPageForm.active = true
      },
      pageChange(page) {
        this.number = page - 1
        this.adminPage()
      },
      pageSizeChange(page) {
        this.size = page
        this.adminPage()
      },
      searchActiveChange(change) {
        this.searchActive = change
        this.adminPage()
      }
    }
  }
</script>

<style scoped>

</style>
